package cn.qdhlm.frontendprocessor.model.memory;


import cn.qdhlm.frontendprocessor.common.util.ComUtil;
import cn.qdhlm.frontendprocessor.common.util.JsonUtil;
import lombok.Data;
import lombok.extern.log4j.Log4j2;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Name: SocketSessionMap
 * @Description: SocketSessionMap
 * @Author : jiangcs
 */
@Log4j2
@Data
public class CollectorSessionMap {
    /**
     * key: collectorNo
     * value: collectorSession
     */
    private static Map<String, CollectorSession> sessionMap = new HashMap<>(1024);

    public static Map<String, CollectorSession> getMap() {
        return sessionMap;
    }

    public static void add(String collectorNo, CollectorSession collectorSession) {
        // 创建时间
        collectorSession.setCreateTime(ComUtil.formatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss"));

        sessionMap.put(collectorNo, collectorSession);

        log.info("collectorSessionMap add: collectorNo[{}] => {}", collectorNo, JsonUtil.safeJsonStr(collectorSession));
    }

    public static CollectorSession get(String collectorNo) {
        return sessionMap.get(collectorNo);
    }

    public static CollectorSession remove(String collectorNo) {
        CollectorSession collectorSession = sessionMap.remove(collectorNo);

        log.info("collectorSessionMap remove: collectorNo[{}] => {}", collectorNo, JsonUtil.safeJsonStr(collectorSession));

        return collectorSession;
    }
}
